package com.app.newcsc.data;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

//import com.goldze.mvvmhabit.data.source.HttpDataSource;
//import com.goldze.mvvmhabit.data.source.LocalDataSource;
//import com.goldze.mvvmhabit.entity.DemoEntity;

import com.app.newcsc.data.source.HttpDataSource;
import com.app.newcsc.data.source.LocalDataSource;
import com.app.newcsc.entity.Article;
import com.app.newcsc.entity.ArticleEntity;
import com.app.newcsc.entity.AssetIndexEntity;
import com.app.newcsc.entity.BillEntity;
import com.app.newcsc.entity.ConfigEntity;
import com.app.newcsc.entity.DemoEntity;
import com.app.newcsc.entity.DetailEntity;
import com.app.newcsc.entity.ExchangeCoinEntity;
import com.app.newcsc.entity.ExchangeHomeEntity;
import com.app.newcsc.entity.ExchangeLogEntity;
import com.app.newcsc.entity.ExchangeRecordEntity;
import com.app.newcsc.entity.FinanceOrderAllEntity;
import com.app.newcsc.entity.FinancingOrderEntity;
import com.app.newcsc.entity.FiveBuyFiveSellEntity;
import com.app.newcsc.entity.FundsBalanceEntity;
import com.app.newcsc.entity.GetTokenEntity;
import com.app.newcsc.entity.LevelInfoEntity;
import com.app.newcsc.entity.OrderListEntity;
import com.app.newcsc.entity.OtcConfig;
import com.app.newcsc.entity.PayAccountEntity;
import com.app.newcsc.entity.PlanIndexEntity;
import com.app.newcsc.entity.PointEntity;
import com.app.newcsc.entity.QiniuToken;
import com.app.newcsc.entity.ShopTokenEntity;
import com.app.newcsc.entity.SityServiceLogEntity;
import com.app.newcsc.entity.TaskInfoEntity;
import com.app.newcsc.entity.TimeNumZone;
import com.app.newcsc.entity.TransactionOrderEntity;
import com.app.newcsc.entity.TransactionPlanEntity;
import com.app.newcsc.entity.TransactionPlanListData;
import com.app.newcsc.entity.UpGradeEntity;
import com.app.newcsc.entity.UpdateCongigEntity;
import com.app.newcsc.entity.UploadFileEntity;
import com.app.newcsc.entity.UserEntity;
import com.app.newcsc.entity.UserInfoEntity;
import com.app.newcsc.entity.WalletEntity;
import com.app.newcsc.entity.WalletTypeEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.http.BaseResponse;
import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.http.QueryMap;

/**
 * MVVM的Model层，统一模块的数据仓库，包含网络数据和本地数据（一个应用可以有多个Repositor）
 * Created by goldze on 2019/3/26.
 */
public class DemoRepository extends BaseModel implements HttpDataSource, LocalDataSource {
    private volatile static DemoRepository INSTANCE = null;
    private final HttpDataSource mHttpDataSource;

    private final LocalDataSource mLocalDataSource;

    private DemoRepository(@NonNull HttpDataSource httpDataSource,
                           @NonNull LocalDataSource localDataSource) {
        this.mHttpDataSource = httpDataSource;
        this.mLocalDataSource = localDataSource;
    }

    public static DemoRepository getInstance(HttpDataSource httpDataSource,
                                             LocalDataSource localDataSource) {
        if (INSTANCE == null) {
            synchronized (DemoRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DemoRepository(httpDataSource, localDataSource);
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }



    //*******************基础接口****************************

    /** 上传文件
     * type  string
     * file  ($binary)
     * @param
     * @param file
     * @return
     */
    @Override
    public Observable<BaseResponse<UploadFileEntity>> uploadFile(String type,MultipartBody.Part file){
        return mHttpDataSource.uploadFile(type,file);
    }

    /** 发送短信验证码
     * type:手机验证码类型：1:注册，2:找回密码，3:更换手机号, 4:添加银行卡, 5:设置支付密码,6: 登陆, 11: otc支付, 12: 挂单 用户, 13:挂单 订单
     * Request body        phone   type
     * @return
     */
    @Override
    public Observable<ResponseBody> smsCode(HashMap<String, Object> map){
        return mHttpDataSource.smsCode(map);
    }

    /** 获取验证码
     * @return
     */
    @Override
    public Observable<BaseResponse> captcha(){
        return mHttpDataSource.captcha();
    }

    /** 缩略图
     * template 图片处理模板，默认有 small,medium,large三种
     * image_path 储存的文件名，程序保存时的路径
     * @return
     */
    @Override
    public Observable<BaseResponse> captcha(String template, String image_path){
        return mHttpDataSource.captcha(template,image_path);
    }

    /** 配置信息
     * template 图片处理模板，默认有 small,medium,large三种
     * image_path 储存的文件名，程序保存时的路径
     * @return
     */
    @Override
    public Observable<BaseResponse<ConfigEntity>> apiConfigs(){
        return mHttpDataSource.apiConfigs();
    }

    /** 获取知天下商城token
     * @return
     */
    @Override
    public Observable<BaseResponse<ShopTokenEntity>> getZTXShopToken(String mobile)
    {
        return mHttpDataSource.getZTXShopToken(mobile);
    }

    /** 积分兑换AD或CSC
     * @return
     */
    @Override
    public Observable<ResponseBody> pointExchangeCsc(HashMap<String, Object> map)
    {
        return mHttpDataSource.pointExchangeCsc(map);
    }

    /** 获取积分
     * @return
     */
    @Override
    public Observable<PointEntity> pointWallet()
    {
        return mHttpDataSource.pointWallet();
    }
//*******************基础接口****************************

    //---------------------------用户-----------------------------
    @Override
    public Observable<BaseResponse<ShopTokenEntity>> login(HashMap<String, Object> map) {
        return mHttpDataSource.login(map);
    }
    /**http://newcsc.chaishows.com/api/base/user
     * 当期登录用户信息
     * @return
     */
    @Override
    public Observable<BaseResponse<UserEntity>> baseUser(){
        return mHttpDataSource.baseUser();
    }
    /**http://newcsc.chaishows.com/api/base/user/password
     * 重置密码
     * @return
     */
    @Override
    public Observable<Response<Void>> reSetUserPassword(HashMap<String, Object> map){
        return mHttpDataSource.reSetUserPassword(map);
    }

    /**
     * 用户登出
     * @return
     */
    @Override
    public Observable<ResponseBody> logoutCurrent(){
        return mHttpDataSource.logoutCurrent();
    }
    //---------------------------用户-----------------------------

    @Override
    public Observable<BaseResponse> getCSCToken(String token) {
        return mHttpDataSource.getCSCToken(token);
    }

    @Override
    public Observable<BaseResponse<List<FinanceOrderAllEntity>>> financeProduct(HashMap<String, Object> map) {
        return mHttpDataSource.financeProduct(map);
    }
    @Override
    public Observable<BaseResponse> financeProductDetail(String id){
        return mHttpDataSource.financeProductDetail(id);
    }

    @Override
    public Observable<BaseResponse<List<FinanceOrderAllEntity>>> financeProductOrders(String status, String include,HashMap<String, Object> map){
        return mHttpDataSource.financeProductOrders(status,include,map);
    }

    /**
     * 任务包 创建订单
     * id  产品id；  quantity 数量；payment_password 支付密码
     * @return
     */
    @Override
    public Observable<ResponseBody> createFinanceProductOrder(HashMap<String, Object> map){
        return mHttpDataSource.createFinanceProductOrder(map);
    }

    /**
     * 任务包 订单详情
     * @return
     */
    @Override
    public Observable<BaseResponse> financeProductOrderDetails(String id,String include){
        return mHttpDataSource.financeProductOrderDetails(id,include);
    }

    /**
     * 文章接口
     * @return
     */
    @Override
    public Observable<BaseResponse<Article>> zdeskArtivle(String id){
        return mHttpDataSource.zdeskArtivle(id);
    }

//--------------------钱包日志记录-----------------

    /**http://newcsc.chaishows.com/api/otc/bill?type=100
     * 钱包日志记录
     * @param
     * @param type 类型, 多个英文逗号隔开
     * @return
     */
    @Override
    public Observable<BaseResponse<List<BillEntity>>> otcBill(String type, String include,String page){
        return mHttpDataSource.otcBill(type,include,page);
    }

    /**http://newcsc.chaishows.com/api/otc/bill/1
     * 钱包日志详情
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse> otcBillDetail(String id){
        return mHttpDataSource.otcBillDetail(id);
    }

    /**http://newcsc.chaishows.com/api/otc/bill/types
     * 钱包类型列表
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<WalletTypeEntity>>> otcBillTypes(){
        return mHttpDataSource.otcBillTypes();
    }
    /**http://newcsc.chaishows.com/api/finance/fund/coin
     * 兑换币种
     * @return
     */
    @Override
    public Observable<BaseResponse<ExchangeCoinEntity>> exchangeCoin(){
        return mHttpDataSource.exchangeCoin();
    }
    /**http://newcsc.chaishows.com/api/otc/exchange_log?include=filter[1]
     * 兑换记录
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<ExchangeLogEntity>>> exChangeLog(String include,String page){
        return mHttpDataSource.exChangeLog(include,page);
    }

//--------------------钱包日志记录-----------------

//--------------------钱包-----------------
    /**http://newcsc.chaishows.com/api/otc/wallet/coins
     * 钱包类型 列表
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<Map<String,String>> walletCoins(){
        return mHttpDataSource.walletCoins();
    }

    /**http://newcsc.chaishows.com/api/otc/wallet ?include=filter[1]
     * 钱包余额
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<WalletEntity>>> otcWallet(String include){
        return mHttpDataSource.otcWallet(include);
    }
    @Override
    public Observable<BaseResponse<List<BillEntity>>> walletLog(String page) {
        return mHttpDataSource.walletLog(page);
    }

//--------------------钱包-----------------





    //--------------------otc buy-----------------

    /**http://newcsc.chaishows.com/api/otc/buy/lists?filter[status]=0
     * 我的订单记录
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<TransactionPlanEntity>>> otcBuyLists(String status,String page){
        return mHttpDataSource.otcBuyLists(status,page);
    }

    /**http://newcsc.chaishows.com/api/ohttp://newcsc.chaishows.com/api/otc/buy/show/229555
     * 我的订单详情
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<TransactionOrderEntity>> otcBuyShowOrder(String id){
        return mHttpDataSource.otcBuyShowOrder(id);
    }

    /**http://newcsc.chaishows.com/api/otc/buy?id=001&type=1&num=1&payment_password="123456"
     * 挂单订单 创建
     * id  planUser id , 状态为 0
     * type 类型:1=购买,2=出售
     * num 挂单数量
     * payment_password 支付密码
     * idcard  卡后6位,如果type=2必填
     * code 短信码,如果type=2必填
     * @param
     * @return
     */
    @Override
    public Observable<ResponseBody> otcBuy(String id,String type,String num,String payment_password,String idcard,String code){
        return mHttpDataSource.otcBuy(id, type, num, payment_password, idcard, code);
    }

    /**http://newcsc.chaishows.com/api/otc/buy/confirm?id=001&payment_password="123456"
     * 卖家 确认订单
     * @param
     * @return
     */
    @Override
    public Observable<ResponseBody> otcBuyConfirm(String id,String payment_password){
        return mHttpDataSource.otcBuyConfirm(id, payment_password);
    }
    /**http://newcsc.chaishows.com/api/otc/buy/submit?id=001&payment_password="123456"&pay_id=1&image
     * 买家 确认上传
     * @param
     * @return
     */
    @Override
    public Observable<ResponseBody> otcBuySubmit(String id,String pay_id,String image,String payment_password){
        return mHttpDataSource.otcBuySubmit(id, pay_id,image, payment_password);
    }
    /**http://newcsc.chaishows.com/api/otc/buy/appeal?id=001&content ="1111"&image
     * 仲裁
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<ResponseBody> otcBuyAppeal(String id,String image,String content){
        return mHttpDataSource.otcBuyAppeal(id, image, content);
    }

//--------------------otc buy----------------

//--------------------otc 接口----------------
    /**http://newcsc.chaishows.com/api/otc/config
     * otc 配置信息
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<OtcConfig>> otcConfig(){
        return mHttpDataSource.otcConfig();
    }
    /**http://newcsc.chaishows.com/api/sityservice/log
     * 服务商活跃指数记录
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<SityServiceLogEntity>> sityServiceLog(){
        return mHttpDataSource.sityServiceLog();
    }

//--------------------otc 接口----------------

//--------------------otc 支付记录接口接口----------------
    /**http://newcsc.chaishows.com/api/otc/payaccount
     * 支付记录
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<PayAccountEntity>>> otcPayCccount(){
        return mHttpDataSource.otcPayCccount();
    }
    /**http://newcsc.chaishows.com/api/otc/payaccount
     * 添加支付记录
     * @param
     * @param {"type": 1,"account": "Entity","name": "Unpro","address": "dsfdsfds","branch": "branch","code": 1234}
     * @return
     */
    @Override
    public Observable<ResponseBody> addOtcPayCccount(HashMap<String, Object> map){
        return mHttpDataSource.addOtcPayCccount(map);
    }
    /**http://newcsc.chaishows.com/api/otc/payaccount?id=1
     * 删除支付记录
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<ResponseBody> deleteOtcPayCccount(String id){
        return mHttpDataSource.deleteOtcPayCccount(id);
    }
    /**http://newcsc.chaishows.com/api/otc/payaccount/1
     * 支付记录详情
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse> otcPayCccountDetail(String id){
        return mHttpDataSource.otcPayCccountDetail(id);
    }
//--------------------otc 支付记录接口接口----------------



    //--------------------otc 挂单接口----------------
    /**http://newcsc.chaishows.com/api/otc/plan?filter[status]=0
     * 我的挂单-用户列表  filter筛选: status 订单状态:0=进行中,1=完成 2取消 3 待审核
     *  include   orders
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<TransactionPlanEntity>>> otcPlanLists(String status,String orders, String page)
    {
        return mHttpDataSource.otcPlanLists(status,orders, page);
    }

    /**http://newcsc.chaishows.com/api/otc/plan/orders?is_confirm=1
     * 挂单列表  五买 五卖
     * @param
     *       is_confirm=1 五买  is_confirm=0 五卖
     * @return
     */
    @Override
    public Observable<BaseResponse<List<FiveBuyFiveSellEntity>>> otcPlanOrders(String is_confirm){
        return mHttpDataSource.otcPlanOrders(is_confirm);
    }

    /**http://newcsc.chaishows.com/api/otc/plan/lists?num_zone=100-500&filter[type]=1&filter[status]=0
     * 挂单 大厅  filter 筛选: status 订单状态:0=进行中,1=完成 2取消 3 待审核, type: 1=购买,2=出售
     * @param
     * @param
     * @return
     */
    @Override
    public Observable<BaseResponse<List<TransactionPlanEntity>>> planLists(String type, String status, String num_zone, String page){
        return mHttpDataSource.planLists(type,  status, num_zone, page);
    }



    //*******************************************
    /**
     * 挂单-创建planuser
     *  type  类型:1=购买,2=出售
     *  num   挂单数量
     *  pay_types   1 2 3
     *  payment_password  支付密码
     *  price    价格
     *  idcard   身份证尾号6位,如果type=2必填
     *  code    短信码, 如果type=2必填
     * @return
     */
    @Override
    public Observable<ResponseBody> otcPlanCreate(HashMap<String, Object> map){
        return mHttpDataSource.otcPlanCreate(map);
    }

    /**
     * 取消挂单
     * @return
     */
    @Override
    public Observable<ResponseBody> otcPlanCancel(String id){
        return mHttpDataSource.otcPlanCancel(id);
    }

    /**
     * 挂单详情
     * @return
     */
    @Override
    public Observable<BaseResponse<TransactionOrderEntity>> otcPlanOrderDetail(String id){
        return mHttpDataSource.otcPlanOrderDetail(id);
    }

    /**
     * 挂单用户详情
     * @return
     */
    @Override
    public Observable<BaseResponse<TransactionPlanEntity>> otcPlanOrderUserDetail(String id,String include){
        return mHttpDataSource.otcPlanOrderUserDetail(id,include);
    }
    /**
     * 挂单数据
     * @return
     */
    @Override
    public Observable<PlanIndexEntity> otcPlanData(){
        return mHttpDataSource.otcPlanData();
    }
    //*******************************************
//--------------------otc 挂单接口----------------




    @Override
    public Observable<Object> login() {
        return mHttpDataSource.login();
    }

    @Override
    public Observable<DemoEntity> loadMore() {
        return mHttpDataSource.loadMore();
    }

    @Override
    public Observable<BaseResponse<DemoEntity>> demoGet() {
        return mHttpDataSource.demoGet();
    }

    @Override
    public Observable<BaseResponse<DemoEntity>> demoPost(String catalog) {
        return mHttpDataSource.demoPost(catalog);
    }

    @Override
    public Observable<ArrayList<DetailEntity>> lockLog(String coin_id) {
        return mHttpDataSource.lockLog(coin_id);
    }

    @Override
    public Observable<UpGradeEntity> upGrade() {
        return mHttpDataSource.upGrade();
    }

    @Override
    public Observable<LevelInfoEntity> levelInfo() {
        return mHttpDataSource.levelInfo();
    }

    @Override
    public Observable<BaseResponse<GetTokenEntity>> getToken(String mobile) {
        return mHttpDataSource.getToken(mobile);
    }


    @Override
    public Observable<BaseResponse<TimeNumZone>> otcConfig(HashMap<String, Object> map) {
        return mHttpDataSource.otcConfig(map);
    }

    @Override
    public Observable<BaseResponse<TransactionPlanListData>> planList(HashMap<String, Object> map) {
        return mHttpDataSource.planList(map);
    }

    @Override
    public Observable<BaseResponse<WalletEntity>> planWallet(HashMap<String, Object> map) {
        return mHttpDataSource.planWallet(map);
    }

    @Override
    public Observable<BaseResponse<OrderListEntity>> orderList(HashMap<String, Object> map) {
        return mHttpDataSource.orderList(map);
    }

    @Override
    public Observable<BaseResponse<OrderListEntity>> orderGuaDanList(HashMap<String, Object> map) {
        return mHttpDataSource.orderGuaDanList(map);
    }

    @Override
    public Observable<BaseResponse<FundsBalanceEntity>> fundsBalance(HashMap<String, Object> map) {
        return mHttpDataSource.fundsBalance(map);
    }

    @Override
    public Observable<BaseResponse<AssetIndexEntity>> assetIndex(HashMap<String, Object> map) {
        return mHttpDataSource.assetIndex(map);
    }

    @Override
    public Observable<BaseResponse<UserInfoEntity>> userInfo(HashMap<String, Object> map) {
        return mHttpDataSource.userInfo(map);
    }

    @Override
    public Observable<BaseResponse<TaskInfoEntity>> otherAssets(HashMap<String, Object> map) {
        return mHttpDataSource.otherAssets(map);
    }

    @Override
    public Observable<BaseResponse<FinancingOrderEntity>> financingorderList(HashMap<String, Object> map) {
        return mHttpDataSource.financingorderList(map);
    }

    @Override
    public Observable<BaseResponse<FinancingOrderEntity>> financingList(HashMap<String, Object> map) {
        return mHttpDataSource.financingList(map);
    }


    @Override
    public Observable<BaseResponse<UpdateCongigEntity>> apiConfig() {
        return mHttpDataSource.apiConfig();
    }

    @Override
    public Observable<BaseResponse<UpdateCongigEntity>> apiConfig2() {
        return mHttpDataSource.apiConfig2();
    }

    @Override
    public Observable<BaseResponse<List<ArticleEntity>>> getArticle(String id) {
        return mHttpDataSource.getArticle(id);
    }

    @Override
    public Observable<BaseResponse> smsSetpassword(HashMap<String, Object> map) {
        return mHttpDataSource.smsSetpassword(map);
    }

    @Override
    public Observable<BaseResponse> setPassword(HashMap<String, Object> map) {
        return mHttpDataSource.setPassword(map);
    }

    @Override
    public Observable<BaseResponse<SityServiceLogEntity>> sityServiceLog(HashMap<String, Object> map) {
        return mHttpDataSource.sityServiceLog(map);
    }

    @Override
    public Observable<BaseResponse> payAccountList(HashMap<String, Object> map) {
        return mHttpDataSource.payAccountList(map);
    }

    @Override
    public Observable<BaseResponse> smsGeneral(HashMap<String, Object> map) {
        return mHttpDataSource.smsGeneral(map);
    }

    @Override
    public Observable<BaseResponse> payAccountCreate(HashMap<String, Object> map) {
        return mHttpDataSource.payAccountCreate(map);
    }

    @Override
    public Observable<BaseResponse<QiniuToken>> qiniuToken(HashMap<String, Object> map) {
        return mHttpDataSource.qiniuToken(map);
    }

    @Override
    public Observable<BaseResponse<UploadFileEntity>> uploadFiles(String token, MultipartBody.Part file) {
        return mHttpDataSource.uploadFiles(token,file);
    }

    @Override
    public Observable<BaseResponse<PlanIndexEntity>> planIndex(HashMap<String, Object> map) {
        return mHttpDataSource.planIndex(map);
    }

    @Override
    public Observable<BaseResponse<ExchangeHomeEntity>> exchangeHome() {
        return mHttpDataSource.exchangeHome();
    }
    /**http://newcsc.chaishows.com/api/finance/ztx_shop/wallet/exchange?amount=10&coin_id=3&to_coin_id=5&payment_password=123456
     * AD兑换积分
     * @return
     */
    @Override
    public Observable<Response<Void>> exchange(HashMap<String, Object> map){
        return mHttpDataSource.exchange(map);
    }
    @Override
    public Observable<BaseResponse<ShopTokenEntity>> getShopToken() {
        return mHttpDataSource.getShopToken();
    }

    @Override
    public Observable<BaseResponse<ExchangeRecordEntity>> exchangeRecord(@QueryMap HashMap<String, Object> map) {
        return mHttpDataSource.exchangeRecord(map);
    }

    @Override
    public void saveUserName(String userName) {
        mLocalDataSource.saveUserName(userName);
    }

    @Override
    public void savePassword(String password) {
        mLocalDataSource.savePassword(password);
    }

    @Override
    public String getUserName() {
        return mLocalDataSource.getUserName();
    }

    @Override
    public String getPassword() {
        return mLocalDataSource.getPassword();
    }
}
